﻿using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using WPFExcelReport.Helpers;
namespace WPFExcelReport
{
    public partial class TSDDAT
    {

        public TSDDAT()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VSDDAT();
            this.DataContext = MV;
            InitializeComponent();
        } 

        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
            if (dwMaster == null)
            {
                return;
            }
        }

        public override void QueryCommand(object sender, System.Windows.RoutedEventArgs e)
        {
            if (SDSA == null)
                return;
            if (SDSA.Trim() == "")
                return;
            ((VSDDAT)MV).SetUp(SDSA);
            dwData = TBMain.NewRow();
            dwData["SDSA"] = SDSA;
            dwData["SDKEY"] = " AND SDKEY LIKE '%" + LANG + "'";
            SResult = ((VSDDAT)MV).QuerySDDAT(dwData, System.Convert.ToInt32(SDSA));
            MergeData(SResult);
            base.QueryCommand(sender, e);
        }

        public override void NewCommand(object sender, System.Windows.RoutedEventArgs e)
        {
            if (KEY.Trim().Length == 0)
            {
                MessageBox.Show("需要先键入新键值");
                return;
            }
            try
            {

                dwData = TBMain.NewRow();
                TMV.SetUpNewRow(ref dwData, SDSA);
                dwData["KEY"] = KEY;
                TBMain.Rows.InsertAt(dwData, 0);
                vwMaster.View.MoveCurrentToFirst();
                base.NewCommand(sender, e);
            }
            catch (Exception ex)
            {
                MessageBox.Show("请尝试查询一下再新建");
            }
        }
        public override void CopyCommand(object sender, System.Windows.RoutedEventArgs e)
        {
            if (KEY.Trim().Length == 0)
            {
                MessageBox.Show("需要先键入新键值");
                return;
            }
            if (dwMaster == null)
                return;
            dwData = TBMain.NewRow();
            dwData.ItemArray = dwMaster.ItemArray;
            dwData["KEY"] = KEY;
            TBMain.Rows.InsertAt(dwData, 0);
            vwMaster.View.MoveCurrentToFirst();
            base.CopyCommand(sender, e);
        } 

        private void CopyENCommand(object sender, RoutedEventArgs e)
        {
            if (dwMaster == null)
                return;
            dwData = TBMain.NewRow();
            dwData.ItemArray = dwMaster.ItemArray;
            dwData["SDKEY"] = dwData["SDKEY"].ToString().Substring(0, 23) + "E";
            TBMain.Rows.InsertAt(dwData, 0);
            vwMaster.View.MoveCurrentToFirst();
            base.CopyCommand(sender, e);
        }

        private void ClearSDSACommand(object sender, RoutedEventArgs e)
        {
            if (!app.IsITX)
                return;
            if (dwMaster == null)
                return;
            TMV.DSD00(dwMaster); 
            TBMain.Clear();
            TBMain.AcceptChanges(); 
        }
    }
}
